package com.demo.action;

import com.demo.bean.Emp;
import com.demo.service.EmpService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;
import java.util.List;

/**
 * @author : mafan
 * @date : 2021-02-03 14:04
 **/
public class LoginAction extends ActionSupport {


    private EmpService empService;

    public EmpService getEmpService() {
        return empService;
    }

    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

    /**
     * 员工属性
     */
    Emp emp = new Emp();
    private Integer empno;
    private String ename;
    private String job;
    private Integer sal;
    private Integer mgr;
    private Integer comm;

    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }
    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Integer getSal() {
        return sal;
    }

    public void setSal(Integer sal) {
        this.sal = sal;
    }

    public Integer getMgr() {
        return mgr;
    }

    public void setMgr(Integer mgr) {
        this.mgr = mgr;
    }

    public Integer getComm() {
        return comm;
    }

    public void setComm(Integer comm) {
        this.comm = comm;
    }

    /**
     * 登录
     *
     * @return
     */
    public String loginEmp() {
        System.out.println("登录操作");
        String ename = this.ename;
        List<Emp> emp = empService.findEmp(ename);
        System.out.println("****************");
        System.out.println(emp.toString());
        System.out.println("****************");

        if (emp.size() > 0) {
            System.out.println("————成功" + ename);
            return SUCCESS;
        } else {
            System.out.println("————失败");
            return ERROR;
        }
    }

    /**
     * 添加员工
     *
     * @return
     */
    public String addEmp() {
        System.out.println("注册操作");
        String ename = this.ename;
        String job = this.job;
        Integer sal = this.sal;
        emp.setEname(ename);
        emp.setJob(job);
        emp.setSal(sal);

        boolean b = empService.saveEmp(emp);
        if (b == true) {
            System.out.println("添加成功" + emp);
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    /**
     * 查询所有员工
     *
     * @return
     */
    public String findAll() {
        System.out.println("查询所有员工");
        ActionContext ctx = ActionContext.getContext();

        List<Emp> empList = empService.findAll(emp);
        ctx.getSession().put("empList", empList);
        if (empList.size() > 0) {
            System.out.println("————成功");
            return SUCCESS;
        } else {
            System.out.println("————失败");
            return ERROR;
        }
    }

    /**
     * 删除员工
     *
     * @return
     */
    public String deleteEmp() {
        System.out.println("根据empno删除");

        Integer empno = this.empno;
        int del=empService.deleteEmp(empno);
        if (del != 0) {
            System.out.println("————成功");
            return SUCCESS;
        } else {
            System.out.println("————失败");
            return ERROR;
        }
    }

    /**
     * 修改员工信息
     * @return
     */
    public String updateEmp(){
        System.out.println("修改员工信息");

        Integer empno = this.empno;
        String ename = this.ename;
        String job = this.job;
        Integer sal = this.sal;
        Integer mgr = this.mgr;
        Integer comm = this.comm;
        emp.setEmpno(empno);
        emp.setEname(ename);
        emp.setJob(job);
        emp.setSal(sal);
//        emp.setComm(comm);
//        emp.setMgr(mgr);

        boolean b = empService.updateEmp(emp);
        if (b == true) {
            System.out.println("修改成功");
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
}
